﻿// -----------------------------------------------------------------------
// <copyright file="ComparerFormatter.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Sitecore.ChangesTracker.UI
{
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Web.UI.WebControls;
  using Sitecore.ChangesTracker.Archiving;
  using Sitecore.Configuration;
  using Sitecore.Data.Items;
  using Sitecore.Diagnostics;
  using Sitecore.Pipelines;
  using Sitecore.Pipelines.GetContentEditorWarnings;
  using Sitecore.Shell.Applications.ContentEditor.Pipelines.GetContentEditorFields;
  using Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor;
  using Sitecore.Shell.Applications.ContentManager.Dialogs.ResetFields;
  using Sitecore.Web.UI.HtmlControls;
  using Sitecore.Web.UI.WebControls;
  using Sitecore.Text;
  using Sitecore.Web;

  public class ComparerFormatter : Sitecore.Shell.Applications.ContentEditor.EditorFormatter
  {
    private ArchivedItem arcItem;
    //private Item realItem;
    private ViewItemInfo controlPoint;
    public ComparerFormatter(ArchivedItem item,/* Item realItem,*/ ViewItemInfo cp)
    {
      this.arcItem = item;
      //this.realItem = realItem;
      this.controlPoint = cp;
    }
    public override void RenderField(System.Web.UI.Control parent, Shell.Applications.ContentManager.Editor.Field field, Item fieldType, bool readOnly)
    {
      Assert.ArgumentNotNull(parent, "parent");
      Assert.ArgumentNotNull(field, "field");
      Assert.ArgumentNotNull(fieldType, "fieldType");
      
      GridPanel control = new GridPanel
      {
        Width = new Unit(100.0, UnitType.Percentage),
        Columns = 5
      };
      Context.ClientPage.AddControl(parent, control);
      Border border = new Border();
      Context.ClientPage.AddControl(control, border);
      control.SetExtensibleProperty(border, "width", "24");
      control.SetExtensibleProperty(border, "valign", "top");
     
        Space space = new Space("8", "1");
      
      
      border = new Border();
      Context.ClientPage.AddControl(control, border);
      control.SetExtensibleProperty(border, "width", "50%");
      control.SetExtensibleProperty(border, "valign", "top");
      base.RenderField(border, field, fieldType, true);
      space = new Space("8", "1");
      Context.ClientPage.AddControl(control, space);
      border = new Border();
      Context.ClientPage.AddControl(control, border);
      control.SetExtensibleProperty(border, "width", "50%");
      control.SetExtensibleProperty(border, "valign", "top");
      string arcValue = string.Empty;
      ArchivedField aField = null;
      
      if (field.TemplateField.IsShared)
      {
        aField = arcItem.Fields.FirstOrDefault(f => f.SharingType == "shared" && f.FieldId == field.TemplateField.ID.ToGuid());
      }
      else
      {
        if (field.TemplateField.IsUnversioned)
        {
          aField = arcItem.Fields.FirstOrDefault(f => f.SharingType == "unversioned" && f.FieldId == field.TemplateField.ID.ToGuid() && this.controlPoint.Lnaguage == f.Language);
        }
        else
        {
          List<ArchivedField> test = arcItem.Fields.Where(f => f.FieldId == field.TemplateField.ID.ToGuid() && f.SharingType == "versioned" && f.Language == this.controlPoint.Lnaguage).ToList();
          if (test.Count > 0)
          {

            int version = this.controlPoint.Version;
            if (version == 0)
            {

              version = test.Where(f => f.FieldId == field.TemplateField.ID.ToGuid()).Max(delegate(ArchivedField fld)
              {
                return fld.Version;
              });
            }
            aField = test.FirstOrDefault(f => this.controlPoint.Lnaguage == f.Language && version == f.Version);
            //aField = arcItem.Fields.FirstOrDefault(f => f.SharingType == "versioned" && f.FieldId == field.TemplateField.ID.ToGuid() && this.controlPoint.Lnaguage == f.Language && version == f.Version);
          }
        }
      }

      if (aField != null)
      {
        arcValue = aField.Value;
      }
        Sitecore.Shell.Applications.ContentManager.Editor.Field clone= field.Clone(Control.GetUniqueID("RFIELD"));
     
      base.RenderField(border, clone, fieldType, true, StringUtil.GetString(new string[] { arcValue }));
      if (arcValue != field.Value)
      {
          var c=border.FindControl(clone.ControlID).Parent;          
          while (!(c is System.Web.UI.WebControls.WebControl))
          {
              c = c.Parent;
          }
          System.Web.UI.WebControls.WebControl ctl = c as System.Web.UI.WebControls.WebControl;
          ctl.Style.Add("color", " Red");
          ctl.Style.Add("border", "1px solid Red");
          //ctl.BorderColor = System.Drawing.Color.Red;
          //ctl.BorderStyle = BorderStyle.Solid;
          //ctl.BorderWidth = 2;
          ctl.Attributes.Remove("disabled");
      }
    }
  }
 
}
